package io.renren.modules.generator.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vortex.tool.httpclient.HttpMethodName;
import com.vortex.tool.httpclient.Protocol;
import com.vortex.tool.httpclient.VtxHttpClient;
import com.vortex.tool.httpclient.request.VtxHttpRequest;
import com.vortex.tool.httpclient.sign.VtxV1Signer;
import io.renren.common.utils.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.constant.DeviceFinal;
import io.renren.modules.generator.dao.DeviceAllDao;
import io.renren.modules.generator.dao.DeviceSiteDao;
import io.renren.modules.generator.dto.DeviceWarningQuotaMainDto;
import io.renren.modules.generator.dto.DeviceWarningQuotaSubDto;
import io.renren.modules.generator.entity.*;
import io.renren.modules.generator.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.*;

import static io.renren.constant.DeviceFinal.device_all_code_map;

@Slf4j
@Service("deviceAllService")
public class DeviceAllServiceImpl extends ServiceImpl<DeviceAllDao, DeviceAllEntity> implements DeviceAllService {
    private static Logger logger = LoggerFactory.getLogger(DeviceAllServiceImpl.class);
    @Autowired
    private DeviceHourService deviceHourService;
    @Autowired
    private DeviceAllService deviceAllService;
    @Autowired
    private DeviceAllDao deviceAllDao;
    @Autowired
    private DeviceDayService deviceDayService;
    @Autowired
    private DeviceYearService deviceYearService;
    @Autowired
    private DeviceWarningQuotaService deviceWarningQuotaService;
    @Autowired
    private DeviceSiteDao deviceSiteDao;

    @Override
    public Map<String, DeviceWarningQuotaMainDto> actualquota(String[] deviceCodes) {
        Map<String, DeviceWarningQuotaMainDto> resultMap = null;
        log.info("deviceCodes:"+deviceCodes);
        VtxHttpRequest request = new VtxHttpRequest();
        request.withHttpMethod(HttpMethodName.GET);
        request.withParams("deviceIds", deviceCodes);
        try {
            request.withUri(new URI(DeviceFinal.device_url));
            VtxHttpClient client = new VtxHttpClient(DeviceFinal.credential, new VtxV1Signer(), Protocol.HTTP);
            String result = client.execute(request);
            JSONObject jsonObject = JSONObject.parseObject(result);

            resultMap = new HashMap<>();
            DeviceSiteEntity deviceSiteEntity;
            QueryWrapper<DeviceSiteEntity> queryWrapper;
            DeviceWarningQuotaMainDto deviceWarningQuotaMainDto;
            DeviceWarningQuotaSubDto deviceWarningQuotaSubDto;
            List<DeviceWarningQuotaSubDto> deviceActualQuotaList;
            for (int i = 0; i < deviceCodes.length; i++) {
                deviceActualQuotaList = new ArrayList<>();
                JSONArray jsonArray = jsonObject.getJSONObject("ret").getJSONArray(deviceCodes[i]);
                queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(DeviceSiteEntity::getDeviceCode,deviceCodes[i]);
                deviceSiteEntity = deviceSiteDao.selectOne(queryWrapper);
                for (int j = 0; j < jsonArray.size(); j++) {
                    deviceWarningQuotaSubDto =
                            DeviceWarningQuotaSubDto.builder()
                            .deviceCode(jsonArray.getJSONObject(j).getString("code"))
                            .deviceCodeName(DeviceFinal.allQuotaMap.get(jsonArray.getJSONObject(j).getString("code")))
                            .actualQuota(jsonArray.getJSONObject(j).getString("value"))
                            .build();
                    deviceActualQuotaList.add(deviceWarningQuotaSubDto);
                }
                deviceWarningQuotaMainDto =
                        DeviceWarningQuotaMainDto.builder()
                        .deviceWarningQuotaSubDtoList(deviceActualQuotaList)
                        .deviceEncrypt(deviceCodes[i])
                        .deviceSiteName(deviceSiteEntity.getSiteName())
                        .build();
                resultMap.put(deviceCodes[i], deviceWarningQuotaMainDto);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DeviceAllEntity> page = this.page(
                new Query<DeviceAllEntity>().getPage(params),
                new QueryWrapper<DeviceAllEntity>()
        );

        return new PageUtils(page);
    }


    @Override
    public void putStatisticsDataForHour() {
        logger.debug("------------putStatisticsDataForHour start");
        try{
            Calendar calendar = Calendar.getInstance();
            /* HOUR_OF_DAY 指示一天中小时 */
            calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);//获取前一个小时
            /* MINUTE 指示一天中的某分 */
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
            String beforeHour = df.format(calendar.getTime());
            Date date = df.parse(df.format(calendar.getTime()));
            Map<String,String> queryMap = new HashMap<>();
            DeviceHourEntity deviceHourEntity;
            List<String> ids = new ArrayList<>();
            List<DeviceHourEntity> deviceHourEntityList = new ArrayList<>();
            for (String code: DeviceFinal.device_gas_code_arr){//气体平均小时入库
                for (Map.Entry<String, String> entry : DeviceFinal.gasQuotaMap.entrySet()){
                    queryMap.put("code",code);
                    queryMap.put("beforeHour",beforeHour);
                    queryMap.put("quota",entry.getKey());
                    String avgQuota = deviceAllService.selectAvgQuota(queryMap);
                    logger.debug("------------device_gas_code_arr putStatisticsDataForHour code:" + entry.getKey() + ",quota:" + entry.getValue() + ",avgQuota:"+avgQuota);
                    if(StringUtils.isEmpty(avgQuota)){
                        continue;
                    }
                    BigDecimal avgQuotaBig = new BigDecimal(avgQuota); // 利息
                    avgQuota = avgQuotaBig.setScale(2, RoundingMode.HALF_UP).toString();
                    deviceHourEntity = new DeviceHourEntity();
                    deviceHourEntity.setCreateTime(date);
                    deviceHourEntity.setDeviceEncrypt(code);
                    deviceHourEntity.setDeviceCode(entry.getKey());
                    deviceHourEntity.setDeviceCodeTime(device_all_code_map.get(code)+"-"+deviceHourEntity.getDeviceCode()+deviceHourEntity.getCreateTime().getTime());
                    deviceHourEntity.setDeviceId(DeviceFinal.device_all_code_map.get(code));
                    deviceHourEntity.setDeviceIndexName(entry.getValue());
                    deviceHourEntity.setDeviceValue(avgQuota);
                    ids.add(deviceHourEntity.getDeviceCodeTime());
                    deviceHourEntityList.add(deviceHourEntity);
                    deviceWarningQuotaService.putHourWarningQuota(deviceHourEntity);
                    logger.debug("------------device_gas_code_arr putStatisticsDataForHour deviceHourEntity.id:" + deviceHourEntity.getDeviceCodeTime());
                    logger.debug("------------device_gas_code_arr putStatisticsDataForHour deviceHourEntity:" + deviceHourEntity);
                    deviceWarningQuotaService.putHourWarningQuota(deviceHourEntity);//监测是否有小时平均预警
                }

            }
            logger.debug("------------putStatisticsDataForHour ids:" + JSON.toJSONString(ids));
            logger.debug("------------putStatisticsDataForHour dayEntityList:" + JSON.toJSONString(deviceHourEntityList));
            deviceHourService.removeByIds(ids);
            deviceHourService.saveBatch(deviceHourEntityList);
            ids = new ArrayList<>();
            deviceHourEntityList = new ArrayList<>();
            for (String code: DeviceFinal.device_water_code_arr){//气体平均小时入库
                for (Map.Entry<String, String> entry : DeviceFinal.waterQuotaMap.entrySet()){
                    queryMap.put("code",code);
                    queryMap.put("beforeHour",beforeHour);
                    queryMap.put("quota",entry.getKey());
                    String avgQuota = deviceAllService.selectAvgQuota(queryMap);
                    logger.debug("------------device_water_code_arr putStatisticsDataForHour code:" + entry.getKey() + ",quota:" + entry.getValue() + ",avgQuota:"+avgQuota);
                    if(StringUtils.isEmpty(avgQuota)){
                        continue;
                    }
                    BigDecimal avgQuotaBig = new BigDecimal(avgQuota); // 利息
                    avgQuota = avgQuotaBig.setScale(2, RoundingMode.HALF_UP).toString();
                    deviceHourEntity = new DeviceHourEntity();
                    deviceHourEntity.setCreateTime(date);
                    deviceHourEntity.setDeviceEncrypt(code);
                    deviceHourEntity.setDeviceCode(entry.getKey());
                    deviceHourEntity.setDeviceCodeTime(device_all_code_map.get(code)+"-"+deviceHourEntity.getDeviceCode()+deviceHourEntity.getCreateTime().getTime());
                    deviceHourEntity.setDeviceId(DeviceFinal.device_all_code_map.get(code));
                    deviceHourEntity.setDeviceIndexName(entry.getValue());
                    deviceHourEntity.setDeviceValue(avgQuota);
                    ids.add(deviceHourEntity.getDeviceCodeTime());
                    deviceHourEntityList.add(deviceHourEntity);
                    logger.debug("------------device_water_code_arr putStatisticsDataForHour deviceHourEntity.id:" + deviceHourEntity.getDeviceCodeTime());
                    logger.debug("------------device_water_code_arr putStatisticsDataForHour deviceHourEntity:" + deviceHourEntity);
                    deviceWarningQuotaService.putHourWarningQuota(deviceHourEntity);//监测是否有小时平均预警
                }
            }
            logger.debug("------------putStatisticsDataForHour ids:" + JSON.toJSONString(ids));
            logger.debug("------------putStatisticsDataForHour dayEntityList:" + JSON.toJSONString(deviceHourEntityList));
            deviceHourService.removeByIds(ids);
            deviceHourService.saveBatch(deviceHourEntityList);
            ids = new ArrayList<>();
            deviceHourEntityList = new ArrayList<>();
            for (String code: DeviceFinal.device_pm25_code_arr){//气体平均小时入库
                for (Map.Entry<String, String> entry : DeviceFinal.pm25QuotaMap.entrySet()){
                    queryMap.put("code",code);
                    queryMap.put("beforeHour",beforeHour);
                    queryMap.put("quota",entry.getKey());
                    String avgQuota = deviceAllService.selectAvgQuota(queryMap);
                    logger.debug("------------device_pm25_code_arr putStatisticsDataForHour code:" + entry.getKey() + ",quota:" + entry.getValue() + ",avgQuota:"+avgQuota);
                    if(StringUtils.isEmpty(avgQuota)){
                        continue;
                    }
                    BigDecimal avgQuotaBig = new BigDecimal(avgQuota); // 利息
                    avgQuota = avgQuotaBig.setScale(2, RoundingMode.HALF_UP).toString();
                    deviceHourEntity = new DeviceHourEntity();
                    deviceHourEntity.setCreateTime(date);
                    deviceHourEntity.setDeviceEncrypt(code);
                    deviceHourEntity.setDeviceCode(entry.getKey());
                    deviceHourEntity.setDeviceCodeTime(device_all_code_map.get(code)+"-"+deviceHourEntity.getDeviceCode()+deviceHourEntity.getCreateTime().getTime());
                    deviceHourEntity.setDeviceId(DeviceFinal.device_all_code_map.get(code));
                    deviceHourEntity.setDeviceIndexName(entry.getValue());
                    deviceHourEntity.setDeviceValue(avgQuota);
                    ids.add(deviceHourEntity.getDeviceCodeTime());
                    deviceHourEntityList.add(deviceHourEntity);
                    logger.debug("------------device_pm25_code_arr putStatisticsDataForHour deviceHourEntity.id:" + deviceHourEntity.getDeviceCodeTime());
                    logger.debug("------------device_pm25_code_arr putStatisticsDataForHour deviceHourEntity:" + deviceHourEntity);
                    deviceWarningQuotaService.putHourWarningQuota(deviceHourEntity);//监测是否有小时平均预警
                }
            }
            logger.debug("------------putStatisticsDataForHour ids:" + JSON.toJSONString(ids));
            logger.debug("------------putStatisticsDataForHour dayEntityList:" + JSON.toJSONString(deviceHourEntityList));
            deviceHourService.removeByIds(ids);
            deviceHourService.saveBatch(deviceHourEntityList);
            ids = new ArrayList<>();
            deviceHourEntityList = new ArrayList<>();
            for (String code: DeviceFinal.device_combalarm_code_arr){//可燃气体平均小时入库
                for (Map.Entry<String, String> entry : DeviceFinal.combalarmQuotaMap.entrySet()){
                    queryMap.put("code",code);
                    queryMap.put("beforeHour",beforeHour);
                    queryMap.put("quota",entry.getKey());
                    String avgQuota = deviceAllService.selectAvgQuota(queryMap);
                    logger.debug("------------device_combalarm_code_arr putStatisticsDataForHour code:" + entry.getKey() + ",quota:" + entry.getValue() + ",avgQuota:"+avgQuota);
                    if(StringUtils.isEmpty(avgQuota)){
                        continue;
                    }
                    BigDecimal avgQuotaBig = new BigDecimal(avgQuota); // 利息
                    avgQuota = avgQuotaBig.setScale(2, RoundingMode.HALF_UP).toString();
                    deviceHourEntity = new DeviceHourEntity();
                    deviceHourEntity.setCreateTime(date);
                    deviceHourEntity.setDeviceEncrypt(code);
                    deviceHourEntity.setDeviceCode(entry.getKey());
                    deviceHourEntity.setDeviceCodeTime(device_all_code_map.get(code)+"-"+deviceHourEntity.getDeviceCode()+deviceHourEntity.getCreateTime().getTime());
                    deviceHourEntity.setDeviceId(DeviceFinal.device_all_code_map.get(code));
                    deviceHourEntity.setDeviceIndexName(entry.getValue());
                    deviceHourEntity.setDeviceValue(avgQuota);
                    ids.add(deviceHourEntity.getDeviceCodeTime());
                    deviceHourEntityList.add(deviceHourEntity);
                    logger.debug("------------device_combalarm_code_arr putStatisticsDataForHour deviceHourEntity.id:" + deviceHourEntity.getDeviceCodeTime());
                    logger.debug("------------device_combalarm_code_arr putStatisticsDataForHour deviceHourEntity:" + deviceHourEntity);
                    deviceWarningQuotaService.putHourWarningQuota(deviceHourEntity);//监测是否有小时平均预警
                }
            }
            logger.debug("------------putStatisticsDataForHour ids:" + JSON.toJSONString(ids));
            logger.debug("------------putStatisticsDataForHour dayEntityList:" + JSON.toJSONString(deviceHourEntityList));
            deviceHourService.removeByIds(ids);
            deviceHourService.saveBatch(deviceHourEntityList);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void putStatisticsForDays() {
        logger.debug("------------putStatisticsForDays start");
        try{
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
            String nowDay = df.format(calendar.getTime());
            Date date = df.parse(df.format(calendar.getTime()));
            Map<String,String> queryMap = new HashMap<>();
            DeviceDayEntity deviceDayEntity;
            List<String> ids = new ArrayList<>();
            List<DeviceDayEntity> dayEntityList = new ArrayList<>();
            for (Map.Entry<String, String> entry : DeviceFinal.allDayAvgQuotaMap.entrySet()){//所有需按天算的设备及对应指标
                queryMap.put("code",entry.getKey());
                queryMap.put("nowDay",nowDay);
                queryMap.put("quota",entry.getValue());
                String avgQuota = deviceHourService.selectAvgQuota(queryMap);
                logger.debug("------------putStatisticsForDays code:" + entry.getKey() + ",quota:" + entry.getValue() + ",avgQuota:"+avgQuota);
                if(StringUtils.isEmpty(avgQuota)){
                    continue;
                }
                BigDecimal avgQuotaBig = new BigDecimal(avgQuota); // 利息
                avgQuota = avgQuotaBig.setScale(2, RoundingMode.HALF_UP).toString();
                deviceDayEntity = new DeviceDayEntity();
                deviceDayEntity.setCreateTime(date);
                deviceDayEntity.setDeviceEncrypt(entry.getKey());
                deviceDayEntity.setDeviceCode(entry.getValue());
                deviceDayEntity.setDeviceCodeTime(deviceDayEntity.getDeviceEncrypt()+deviceDayEntity.getDeviceCode()+deviceDayEntity.getCreateTime().getTime());
                deviceDayEntity.setDeviceIndexName(DeviceFinal.allQuotaMap.get(entry.getKey()));
                deviceDayEntity.setDeviceValue(avgQuota);
                logger.debug("------------putStatisticsForDays deviceDayEntity.id:" + deviceDayEntity.getDeviceCodeTime());
                logger.debug("------------putStatisticsForDays deviceDayEntity:" + deviceDayEntity);
                ids.add(deviceDayEntity.getDeviceCodeTime());
                dayEntityList.add(deviceDayEntity);
                deviceWarningQuotaService.putDayWarningQuota(deviceDayEntity);//监测是否有24小时平均预警
            }
            logger.debug("------------putStatisticsDataForHour ids:" + JSON.toJSONString(ids));
            logger.debug("------------putStatisticsDataForHour dayEntityList:" + JSON.toJSONString(dayEntityList));
            deviceDayService.removeByIds(ids);
            deviceDayService.saveBatch(dayEntityList);
        }catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("------------putStatisticsForDays end");
    }

    @Override
    public void putStatisticsForYears() {
        logger.debug("------------putStatisticsForYears start");
        try{
            Calendar calendar = Calendar.getInstance();
            Map<String,Object> queryMap = new HashMap<>();
            DeviceYearEntity deviceYearEntity;
            List<String> ids = new ArrayList<>();
            List<DeviceYearEntity> deviceYearEntities = new ArrayList<>();
            for (Map.Entry<String, String> entry : DeviceFinal.allDayAvgQuotaMap.entrySet()){//所有需按天算的设备及对应指标
                queryMap.put("code",entry.getKey());
                queryMap.put("nowYear",calendar.get(Calendar.YEAR));
                queryMap.put("quota",entry.getValue());
                String avgQuota = deviceDayService.selectAvgQuota(queryMap);
                logger.debug("------------putStatisticsForYears code:" + entry.getKey() + ",quota:" + entry.getValue() + ",avgQuota:"+avgQuota);
                if(StringUtils.isEmpty(avgQuota)){
                    continue;
                }
                BigDecimal avgQuotaBig = new BigDecimal(avgQuota); // 利息
                avgQuota = avgQuotaBig.setScale(2, RoundingMode.HALF_UP).toString();
                deviceYearEntity = new DeviceYearEntity();
                deviceYearEntity.setCreateYear(calendar.get(Calendar.YEAR));
                deviceYearEntity.setDeviceEncrypt(entry.getKey());
                deviceYearEntity.setDeviceCode(entry.getValue());
                deviceYearEntity.setDeviceCodeTime(deviceYearEntity.getDeviceCode()+deviceYearEntity.getCreateYear());
                deviceYearEntity.setDeviceIndexName(DeviceFinal.allQuotaMap.get(entry.getKey()));
                deviceYearEntity.setDeviceValue(avgQuota);
                logger.debug("------------putStatisticsForYears deviceYearEntity.id:" + deviceYearEntity.getDeviceCodeTime());
                logger.debug("------------putStatisticsForYears deviceYearEntity:" + deviceYearEntity);
                ids.add(deviceYearEntity.getDeviceCodeTime());
                deviceYearEntities.add(deviceYearEntity);
                deviceWarningQuotaService.putYearWarningQuota(deviceYearEntity);//监测是否有年平均预警
            }
            logger.debug("------------putStatisticsForYears ids:" + JSON.toJSONString(ids));
            logger.debug("------------putStatisticsForYears deviceYearEntity:" + JSON.toJSONString(deviceYearEntities));
            deviceYearService.removeByIds(ids);
            deviceYearService.saveBatch(deviceYearEntities);
        }catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("------------putStatisticsForYears end");
    }



    @Override
    public String selectAvgQuota(Map<String, String> queryMap) {
        return deviceAllDao.selectAvgQuota(queryMap);
    }

    @Override
    public void getRemoteDatas() {
        logger.debug("------------getRemoteDatas start:");
        LambdaQueryWrapper<DeviceAllEntity> wrapper;
        DeviceAllEntity deviceAllEntity;
        List<DeviceAllEntity> deviceAllEntityList;
        List<String> idList;
//        for(Map.Entry<String, Integer> entry : device_all_code_map.entrySet()) {
            idList = new ArrayList<>();
            deviceAllEntityList = new ArrayList<>();
//            String deviceCode = entry.getKey();
//            Integer deviceCount = entry.getValue();
            VtxHttpRequest request = new VtxHttpRequest();
            request.withHttpMethod(HttpMethodName.GET);
            request.withParams("deviceIds", DeviceFinal.device_all_code_arr);//团队水质
            try {
                request.withUri(new URI(DeviceFinal.device_url));
                VtxHttpClient client = new VtxHttpClient(DeviceFinal.credential, new VtxV1Signer(), Protocol.HTTP);
                String result = client.execute(request);
//                logger.debug("------------result:" + result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                logger.debug("------------remote quota start:");
                for (int j = 0; j < DeviceFinal.device_all_code_arr.length; j++) {
                    JSONArray jsonArray = jsonObject.getJSONObject("ret").getJSONArray(DeviceFinal.device_all_code_arr[j]);
                    if(Objects.isNull(jsonArray)) {
                        continue;
                    }
                    logger.debug("------------remote device" + DeviceFinal.device_all_code_arr[j] + " start:");
                    for (int i = 0; i < jsonArray.size(); i++) {
//                            wrapper = new LambdaQueryWrapper<>();
//                            wrapper.eq(DeviceAllEntity::getDeviceCode, jsonArray.getJSONObject(i).getString("code"))
//                            .eq(DeviceAllEntity::getCreateTime, DateUtils.longToDate(Long.parseLong(jsonArray.getJSONObject(i).getString("time")),"yyyy-MM-dd HH"));
//
//                            deviceAllService.remove(wrapper);
                        logger.debug("----------------------------code:"+jsonArray.getJSONObject(i).getString("code"));
                        logger.debug("----------------------------value:"+jsonArray.getJSONObject(i).getString("value"));
                        logger.debug("----------------------------time:"+DateUtils.dateToString(DateUtils.longToDate(Long.parseLong(jsonArray.getJSONObject(i).getString("time")),"yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss"));
                        deviceAllEntity = new DeviceAllEntity();

                        deviceAllEntity.setDeviceEncrypt(DeviceFinal.device_all_code_arr[j]);
                        deviceAllEntity.setDeviceCode(jsonArray.getJSONObject(i).getString("code"));
                        deviceAllEntity.setCreateTime(DateUtils.longToDate(Long.parseLong(jsonArray.getJSONObject(i).getString("time")),"yyyy-MM-dd HH:mm:ss"));
                        deviceAllEntity.setDeviceCodeTime(device_all_code_map.get(DeviceFinal.device_all_code_arr[j])+"-"+jsonArray.getJSONObject(i).getString("code")+deviceAllEntity.getCreateTime().getTime());
                        deviceAllEntity.setDeviceValue(jsonArray.getJSONObject(i).getString("value"));
                        deviceAllEntity.setDeviceId(device_all_code_map.get(DeviceFinal.device_all_code_arr[j]));
                        deviceAllEntity.setDeviceIndexName(DeviceFinal.allQuotaMap.get(deviceAllEntity.getDeviceCode()));
//                            deviceAllService.save(deviceAllEntity);
                        idList.add(deviceAllEntity.getDeviceCodeTime());
                        deviceAllEntityList.add(deviceAllEntity);
                        deviceWarningQuotaService.putActualWarningQuota(deviceAllEntity);//监测是否有实时预警，主要是污水计
                    }
                }
                logger.debug("------------remote quota end");
                deviceAllService.removeByIds(idList);
                deviceAllService.saveBatch(deviceAllEntityList);
//                deviceAllService.saveOrUpdateBatch(deviceAllEntityList);
                logger.debug("------------getRemoteDatas end");
            }catch(Exception e){
                e.printStackTrace();
            }
//        }
    }


}
